package com.white.hot.controller.umpush;

import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.white.hot.controller.umpush.android.AndroidBroadcast;
import com.white.hot.controller.umpush.android.AndroidCustomizedcast;
import com.white.hot.controller.umpush.android.AndroidGroupcast;


public class PushUtils {

	private static final String appkey = "58588e62aed17926570007ec";
	private static final String appMasterSecret = "lldcdo3d8hte5acedb9glqafkyywymvd";
	private static PushClient client = new PushClient();
	
	public static boolean sendPush(Map<String, Object> map) throws Exception {
		AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject tag = new JSONObject();
		tag.put("tag", map.get("tag").toString());
		//tagArray.put(testTag);
		tagArray.add(tag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		groupcast.setFilter(filterJson);
		// 必填 通知栏提示文字
		groupcast.setTicker(map.get("ticker").toString());
		// 必填 通知标题
		groupcast.setTitle(map.get("title").toString());
		// 必填 通知文字描述
		groupcast.setText(map.get("text").toString());
		// 点击之后的后续行为
		//broadcast.goAppAfterOpen();
		groupcast.goActivityAfterOpen("com.zkingsoft.readapp.activities.PersonalMessageActivity");
		// 发送通知还是消息 
		groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		// 可选 true/false 正式/测试模式。测试模式下，只会将消息发给测试设备。
		groupcast.setProductionMode(); 
		// Set customized fields
		/*	
		 * 可选 用户自定义key-value。只对"通知(display_type=notification)"生效。
		 * 可以配合通知到达后, 打开App, 打开URL, 打开Activity使用。
		 * 注意: SDK V1.2.3后开始支持
		 */
		groupcast.setExtraField("attach", map.get("attach") != null ? map.get("attach").toString() : "");
		groupcast.setExtraField("recommendUserId", map.get("recommendUserId") != null ? map.get("recommendUserId").toString() : "");
		return client.send(groupcast);
	}
	
	public static boolean sendAndroidBroadcast(Map<String, Object> map) throws Exception {
		AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
		// 必填 通知栏提示文字
		broadcast.setTicker(map.get("ticker").toString());
		// 必填 通知标题
		broadcast.setTitle(map.get("title").toString());
		// 必填 通知文字描述
		broadcast.setText(map.get("text").toString());
		// 点击之后的后续行为
		//broadcast.goAppAfterOpen();
		broadcast.goActivityAfterOpen("com.zkingsoft.readapp.activities.PersonalMessageActivity");
		// 发送通知还是消息 
		broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		// 可选 true/false 正式/测试模式。测试模式下，只会将消息发给测试设备。
		broadcast.setProductionMode(); 
		// Set customized fields
		/*	
		 * 可选 用户自定义key-value。只对"通知(display_type=notification)"生效。
		 * 可以配合通知到达后, 打开App, 打开URL, 打开Activity使用。
		 * 注意: SDK V1.2.3后开始支持
		 */
		//broadcast.setExtraField("test", "helloworld");
		return client.send(broadcast);
	}
	
	public static boolean sendAndroidCustomizedcast(Map<String, Object> map) throws Exception {
		if(map.get("token") == null) {
			return false;
		}
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
		// TODO Set your alias here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then 
		// use file_id to send customized notification.
		/*
		 * "alias":"xx",		// 可选 当type=customizedcast时,开发者填写自己的alias,友盟根据alias进行反查找,得到对应的device_token。
		 * "alias_type": "xx"	// 可选 当type=customizedcast时,必填。 表示alias的类型。
		 */
		customizedcast.setAlias(map.get("token").toString(), "token");
		// 必填 通知栏提示文字
		customizedcast.setTicker(map.get("ticker").toString());
		// 必填 通知标题
		customizedcast.setTitle(map.get("title").toString());
		// 必填 通知文字描述
		customizedcast.setText(map.get("text").toString());
		// 点击之后的后续行为
		//customizedcast.goAppAfterOpen();
		customizedcast.goActivityAfterOpen("com.zkingsoft.readapp.activities.PersonalMessageActivity");
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		customizedcast.setProductionMode();
		customizedcast.setExtraField("token", map.get("token").toString());
		return client.send(customizedcast);
	}
}
